package com.yunlankeji.stemcells.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.yunlankeji.ganxibaozhijia.R;
import com.yunlankeji.stemcells.model.request.Expert_database;

import java.util.List;

public class Expert_databaseAdapter extends RecyclerView.Adapter<Expert_databaseAdapter.MyViewHolder> implements View.OnClickListener {

    private List<Expert_database> expert_databaseList;

    public Expert_databaseAdapter(List<Expert_database> expert_databaseList) {
        this.expert_databaseList = expert_databaseList;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_exper_database,parent,false);
        MyViewHolder myViewHolder = new MyViewHolder(itemView);

        return myViewHolder;
    }


    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {

        Expert_database expert_database = expert_databaseList.get(position);
        holder.tv_expert_name.setText(expert_databaseList.get(position).getExpert_name());
        holder.tv_expert_position.setText(expert_databaseList.get(position).getExpert_position());
        holder.tv_expert_title.setText(expert_databaseList.get(position).getExpert_title());
        holder.iv_expert_picture.setImageResource(expert_databaseList.get(position).getExpert_picture());

        holder.itemView.setTag(position);
        holder.lt_expert_database.setTag(position);

    }

    @Override
    public int getItemCount() {
        return expert_databaseList.size();
    }

    @Override
    public void onClick(View v) {
        int position = (int) v.getTag();      //getTag()获取数据
        if (mOnItemClickListener != null) {
            switch (v.getId()){
                /*case R.id.lt_address_edit:
                    mOnItemClickListener.onItemClick(v, AddressAdapter.ViewName.PRACTISE, position);
                    break;*/
                default:
                    mOnItemClickListener.onItemClick(v, Expert_databaseAdapter.ViewName.ITEM, position);
                    break;
            }
        }
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {

        TextView tv_expert_name,tv_expert_position,tv_expert_title,tv_expert_picture;
        ImageView iv_expert_picture;
        LinearLayout lt_expert_database;
        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            this.tv_expert_name=itemView.findViewById(R.id.tv_expert_name);
            this.tv_expert_position=itemView.findViewById(R.id.tv_expert_position);
            this.tv_expert_title=itemView.findViewById(R.id.tv_expert_title);
            this.iv_expert_picture=itemView.findViewById(R.id.iv_expert_picture);
            this.lt_expert_database=itemView.findViewById(R.id.item_lt_expert_database);

            lt_expert_database.setOnClickListener(Expert_databaseAdapter.this);
        }
    }
    public enum ViewName {
        ITEM,
        PRACTISE
    }

    public interface OnItemClickListener  {
        void onItemClick(View v, Expert_databaseAdapter.ViewName viewName, int position);
        void onItemLongClick(View v);
    }

    private Expert_databaseAdapter.OnItemClickListener mOnItemClickListener;//声明自定义的接口

    //定义方法并传给外面的使用者
    public void setOnItemClickListener(Expert_databaseAdapter.OnItemClickListener listener) {
        this.mOnItemClickListener  = listener;
    }
}
